package com.shixun.milktea.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shixun.milktea.entity.Consumer;

import com.shixun.milktea.entity.Mt;
import com.shixun.milktea.enums.ResultStatusEnum;
import com.shixun.milktea.mapper.ConsumerMapper;
import com.shixun.milktea.service.ConsumerService;
import com.shixun.milktea.vo.PageVo;
import com.shixun.milktea.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zrs
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/consumer")
@Api(tags = "消费者相关接口")
public class ConsumerController {
    @Autowired
    ConsumerMapper consumerMapper;
    @Autowired
    ConsumerService consumerService;

    @GetMapping("/findAllConsumer")
    @ApiOperation(value = "查询所有用户")
    public ResultVO queryAllConsumer() throws Exception {
        QueryWrapper<Consumer> qw=new QueryWrapper<>();
        List<Consumer> mtList = consumerMapper.selectList(qw);

        return new ResultVO(mtList);
    }


    @PostMapping("/addConsumer")
    @ApiOperation(value = "添加用户信息")
    public ResultVO addConsumer(@RequestBody Consumer consumer) throws Exception {

        int insert = consumerService.register(consumer);
        if (insert==0){
            return new ResultVO(ResultStatusEnum.USER_ALREADY_EXIST);
        }
        return new ResultVO(insert);
    }

    @PutMapping("/updateConsumer")
    @ApiOperation(value = "修改用户信息")

    public ResultVO updateConsumer(@RequestBody Consumer consumer) throws Exception {


        int i = consumerMapper.updateById(consumer);

        return new ResultVO(i);
    }




    @DeleteMapping("/deleteConsumerById")
    @ApiOperation(value = "根据id删除用户信息")
    public ResultVO deleteConsumerById(@RequestParam("consumerId") int consumerId) throws Exception {
        int i = consumerMapper.deleteById(consumerId);
        return new ResultVO(i);
    }
    @GetMapping("/QueryUserlist")
    @ApiOperation(value = "分页查询用户信息")

    public ResultVO QueryUserlist(PageVo pageVo) throws Exception {

        Page<Consumer> jobPage = consumerService.selectPage(pageVo);

        return new ResultVO(jobPage);
    }
    @GetMapping("/moHuQueryLikeConsumer")
    @ApiOperation(value = "搜索框模糊分页根据电话号码查询用户信息")
    public ResultVO moHuQueryLikeConsumer(PageVo pageVo,String consumerTelephone
    ) throws Exception {
        Page<Consumer> mtPage = consumerService.MtQueryList(pageVo,consumerTelephone);
        return new ResultVO(mtPage);
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户小程序登录")
    public ResultVO Login(@RequestParam("username") String username,@RequestParam("password") String password) throws Exception {
        Consumer consumer = consumerService.loadUserByUsername(username, password);
        return new ResultVO(consumer);
    }



}
